#!/bin/sh

# Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


# See https://bitbucket.org/verateam/vera/wiki/Home

EXIT_SUCCESS=0
EXIT_FAILURE=1

cmd='vera++ --error'
cmd="$cmd -R F001" # source files should not use the '\r' (CR) character
cmd="$cmd -R F002 -P max-filename-length=40"
cmd="$cmd -R L004 -P max-line-length=90"
cmd="$cmd -R L005 -P max-consecutive-empty-lines=2"
cmd="$cmd -R L006 -P max-file-length=1000"
cmd="$cmd -R T001" # one-line comments should not have forced continuation
cmd="$cmd -R T004" # some keywords should be immediately followed by a colon
cmd="$cmd -R T005" # break and continue followed by a semicolon
cmd="$cmd -R T007" # semicolons not isolated by spaces or comments
cmd="$cmd -R T009" # ", " is right and " ," is wrong
cmd="$cmd -R T019" # control structures with complete curly-braced block

exit_code=$EXIT_SUCCESS
for file_or_dir in "$@"
do
    if test ! -e "$file_or_dir"
    then
	>&2 echo "$file_or_dir does not exist!"
	exit_code=$EXIT_FAILURE
    else
	files=$(find "$file_or_dir" \( -name '*.h' -o -name '*.c' \))
	echo "$files" | $cmd
	if test $? -ne $EXIT_SUCCESS
	then
	    exit_code=$EXIT_FAILURE
	fi
    fi
done
exit $exit_code
